package com.pc.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pc.entity.Changemoney;
import com.pc.mapper.ChangemoneyMapper;
import com.pc.utils.Top10;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ChangemoneyService {
    @Resource
    private ChangemoneyMapper changemoneyMapper;

    //添加到充值记录
    public boolean addChange(String username,Integer addMoney){

        Changemoney changemoney = new Changemoney();
        changemoney.setUsername(username);
        changemoney.setAddmoney(addMoney);
        Date date = new Date(); SimpleDateFormat dateFormat= new
                SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        changemoney.setCreatedate(dateFormat.format(date));
        int insert = changemoneyMapper.insert(changemoney);
        return insert!=0;
    }

    //得到充值记录
    public List<Changemoney> getOrder(String username){
        QueryWrapper<Changemoney> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return changemoneyMapper.selectList(wrapper);
    }

    //获得每个月的营业额
    public int[]  getMohtnMoney(){

        int[] month =new int[12];
        QueryWrapper<Changemoney> wrapper = new QueryWrapper<>();
        List<Changemoney> changemoneyList = changemoneyMapper.selectList(wrapper);
        for(Changemoney a:changemoneyList) {
            //获得表中记录的月份
            String createdatemonth=a.getCreatedate().substring(5,7);
            switch (createdatemonth) {
                case "01":
                    month[0] = month[0] + a.getAddmoney();
                    break;
                case "02":
                    month[1] = month[1] + a.getAddmoney();
                    break;
                case "03":
                    month[2] = month[2] + a.getAddmoney();
                    break;
                case "04":
                    month[3] = month[3] + a.getAddmoney();
                    break;
                case "05":
                    month[4] = month[4] + a.getAddmoney();
                    break;
                case "06":
                    month[5] = month[5] + a.getAddmoney();
                    break;
                case "07":
                    month[6] = month[6] + a.getAddmoney();
                    break;
                case "08":
                    month[7] = month[7] + a.getAddmoney();
                    break;
                case "09":
                    month[8] = month[8] + a.getAddmoney();
                    break;
                case "10":
                    month[9] = month[9] + a.getAddmoney();
                    break;
                case "11":
                    month[10] = month[10] + a.getAddmoney();
                    break;
                case "12":
                    month[11] = month[11] + a.getAddmoney();
                    break;
            }

        }

        return month;
    }
    //得到消费前10的用户
    public List<Top10> getTop10(){
        List<Object> top10User = changemoneyMapper.getTop10User();
        List<Object> top10Money = changemoneyMapper.getTop10Money();
        ArrayList<Top10> top10s = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Top10 top101 = new Top10();
            top101.setUsername(top10User.get(i)+"");
            top101.setMoney(top10Money.get(i)+"");
            top10s.add(top101);
        }
        return top10s;
    }

    /**
     * 导出消费记录表
     */
    public List<Changemoney> exportexcelChangeMoney(){
        QueryWrapper<Changemoney> wrapper = new QueryWrapper<>();
        return changemoneyMapper.selectList(wrapper);
    }
}
